﻿using System;
using System.Collections.Generic;
using SharpArch.Domain.DomainModel;

namespace ExBlogger.Domain {
    public class Post : Entity {

        private List<Category> _categories;

        protected internal Post() {
            Title = String.Empty;
            Content = String.Empty;
            PostDate = DateTime.Now;
            LastModifyDate = PostDate;
            _categories = new List<Category>();
        }
        
        public virtual string Title { get; protected internal set; }
        public virtual string Content { get;protected internal set; }
        public virtual DateTime PostDate { get; protected internal set; }
        public virtual DateTime LastModifyDate { get; protected internal set; }
        public virtual User PostedBy { get; protected internal set; }
        public virtual Blog PostedOn { get; protected internal set; }

        public virtual bool CommentsClosed { get; protected internal set; }

        public virtual IList<Category> Categories { get { return _categories.AsReadOnly(); } }

        public virtual Post On(Blog blog) {
            if (PostedOn != null)
                throw new ArgumentNullException("blog");
            if(!PostedBy.Equals(blog.Owner))
                throw new InvalidOperationException("Can not post on other user's blog.");
            PostedOn = blog;
            return this;
        }

        protected internal virtual void AddToCategory(Category category) {
            if(category.IsTransient()) throw new InvalidOperationException("Post can not be added to transient category");
            _categories.Add(category);
        }
    }
}
